package com.kaili.basic.service.impl;

/**
 * Created by xxx on 2019-03-07.
 */

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.ReceivableHandleMapper;
import com.kaili.basic.pojo.dto.ReceivableHandleQueryDto;
import com.kaili.basic.pojo.entity.HandleExport;
import com.kaili.basic.pojo.entity.ReceivableExport;
import com.kaili.basic.pojo.entity.ReceivableHandle;
import com.kaili.basic.service.ReceivableHandleService;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author xxx
 * @Copyright: Copyright (c) 2017
 * @Company: 杭州凯立通信有限公司
 */
@Service
@Transactional
public class ReceivableHandleServiceImpl implements ReceivableHandleService {
    @Autowired
    ReceivableHandleMapper receivableHandleMapper;

    @Override
    public int addReceivableHandle(ReceivableHandle receivableHandle) {
        receivableHandleMapper.addReceivableHandle(receivableHandle);
        return 1;
    }

    @Override
    public int deleteReceivableHandles(Map<String,Object> map) {
        receivableHandleMapper.deleteReceivableHandles(map);
        return 1;
    }

    @Override
    public int updateReceivableHandle(ReceivableHandle receivableHandle) {
        receivableHandleMapper.updateReceivableHandle(receivableHandle);
        return 1;
    }

    @Override
    public Page<ReceivableHandle> queryReceivableHandle(Page<ReceivableHandle> page,ReceivableHandleQueryDto receivableHandleQueryDto) {
        List<ReceivableHandle> receivableHandles = receivableHandleMapper.queryReceivableHandle(page,receivableHandleQueryDto);
        return page.setRecords(receivableHandles);
    }

    @Override
    public ReceivableHandle queryReceivableHandleById(Long id) {
        return receivableHandleMapper.queryReceivableHandleById(id);
    }

    @Override
    public ReceivableHandle queryBySerialNumber(String serialNumber) {
        return receivableHandleMapper.queryBySerialNumber(serialNumber);
    }

    @Override
    public int updateByReceiptPay (String serialNumber,Double money) {
        ReceivableHandle receivableHandle = receivableHandleMapper.queryBySerialNumber(serialNumber);
        if(receivableHandle==null){
            return 0;
        }
        ReceivableHandle receivableHandleMy = new ReceivableHandle();
        receivableHandleMy.setId(receivableHandle.getId());
        receivableHandleMy.setReceivedMoney(receivableHandle.getReceivedMoney()+money);
        receivableHandleMy.setResidueMoney(receivableHandle.getResidueMoney()-money);
        receivableHandleMy.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        if(receivableHandleMy.getResidueMoney().doubleValue()<=0.0){
            receivableHandleMy.setStatus(5);
        }else{
            receivableHandleMy.setStatus(4);
        }
        receivableHandleMapper.updateReceivableHandle(receivableHandleMy);
        return 1;
    }

    @Override
    public Long getMaxReceivableHandleId() {
        return receivableHandleMapper.getMaxReceivableHandleId();
    }

    @Override
    public List<ReceivableExport> queryReceivableExport(ReceivableHandleQueryDto receivableHandleQueryDto) {
        return receivableHandleMapper.queryReceivableExport(receivableHandleQueryDto);
    }

    @Override
    public List<HandleExport> queryHandleExport(ReceivableHandleQueryDto receivableHandleQueryDto) {
        return receivableHandleMapper.queryHandleExport(receivableHandleQueryDto);
    }
}
